package org.ns.learn.db.autoconf;

import com.alibaba.fastjson.JSON;
import com.zaxxer.hikari.HikariConfig;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
@ComponentScan("org.ns.learn.db.autoconf")
public class DBZookeeperConfigLoader implements BeanFactoryPostProcessor, ApplicationContextAware, EnvironmentAware {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        String envPath = "db.autoconf.registerServerAddress";
        String registerServerAddress = env.getProperty(envPath);
        System.out.println("dbconfig-zkserver="+registerServerAddress);
//        configurableListableBeanFactory.registerSingleton();

//        HikariConfig hikariConfig = new HikariConfig();
//        hikariConfig.setUsername("root");
//        hikariConfig.setPassword("123456");
//        hikariConfig.setJdbcUrl("jdbc:mysql://127.0.0.1/test");
//        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
//        hikariConfig.setMinimumIdle(2);
//        hikariConfig.setMaximumPoolSize(10);
//        System.out.println("config-db"+JSON.toJSONString(hikariConfig));

        try {
            DBZookeeperConfigService configService = new DBZookeeperConfigService(registerServerAddress,configurableListableBeanFactory);
            configService.loaderConfig();
        } catch (Exception e) {
            System.err.println("配置服务使用失败！" + registerServerAddress);
            e.printStackTrace();
        }

    }

    ApplicationContext applicationContext;
    Environment env;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.env = environment;
    }
}
